// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl;

[JsonConverter(typeof(IntervalsConverter))]
public sealed partial class Intervals
{
	internal Intervals(string variantName, object variant)
	{
		if (variantName is null)
			throw new ArgumentNullException(nameof(variantName));
		if (variant is null)
			throw new ArgumentNullException(nameof(variant));
		if (string.IsNullOrWhiteSpace(variantName))
			throw new ArgumentException("Variant name must not be empty or whitespace.");
		VariantName = variantName;
		Variant = variant;
	}

	internal object Variant { get; }
	internal string VariantName { get; }

	public static Intervals AllOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf intervalsAllOf) => new Intervals("all_of", intervalsAllOf);
	public static Intervals AnyOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf intervalsAnyOf) => new Intervals("any_of", intervalsAnyOf);
	public static Intervals Fuzzy(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy intervalsFuzzy) => new Intervals("fuzzy", intervalsFuzzy);
	public static Intervals Match(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch intervalsMatch) => new Intervals("match", intervalsMatch);
	public static Intervals Prefix(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix intervalsPrefix) => new Intervals("prefix", intervalsPrefix);
	public static Intervals Wildcard(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard intervalsWildcard) => new Intervals("wildcard", intervalsWildcard);

	public bool TryGet<T>([NotNullWhen(true)] out T? result) where T : class
	{
		result = default;
		if (Variant is T variant)
		{
			result = variant;
			return true;
		}

		return false;
	}
}

internal sealed partial class IntervalsConverter : JsonConverter<Intervals>
{
	public override Intervals Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
		{
			throw new JsonException("Expected start token.");
		}

		object? variantValue = default;
		string? variantNameValue = default;
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token.");
			}

			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token representing the name of an Elasticsearch field.");
			}

			var propertyName = reader.GetString();
			reader.Read();
			if (propertyName == "all_of")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "any_of")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "fuzzy")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "match")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "prefix")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "wildcard")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			throw new JsonException($"Unknown property name '{propertyName}' received while deserializing the 'Intervals' from the response.");
		}

		var result = new Intervals(variantNameValue, variantValue);
		return result;
	}

	public override void Write(Utf8JsonWriter writer, Intervals value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.VariantName is not null && value.Variant is not null)
		{
			writer.WritePropertyName(value.VariantName);
			switch (value.VariantName)
			{
				case "all_of":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf)value.Variant, options);
					break;
				case "any_of":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf)value.Variant, options);
					break;
				case "fuzzy":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy)value.Variant, options);
					break;
				case "match":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch)value.Variant, options);
					break;
				case "prefix":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix)value.Variant, options);
					break;
				case "wildcard":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard)value.Variant, options);
					break;
			}
		}

		writer.WriteEndObject();
	}
}

public sealed partial class IntervalsDescriptor<TDocument> : SerializableDescriptor<IntervalsDescriptor<TDocument>>
{
	internal IntervalsDescriptor(Action<IntervalsDescriptor<TDocument>> configure) => configure.Invoke(this);

	public IntervalsDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private IntervalsDescriptor<TDocument> Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private IntervalsDescriptor<TDocument> Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public IntervalsDescriptor<TDocument> AllOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf intervalsAllOf) => Set(intervalsAllOf, "all_of");
	public IntervalsDescriptor<TDocument> AllOf(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOfDescriptor<TDocument>> configure) => Set(configure, "all_of");
	public IntervalsDescriptor<TDocument> AnyOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf intervalsAnyOf) => Set(intervalsAnyOf, "any_of");
	public IntervalsDescriptor<TDocument> AnyOf(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOfDescriptor<TDocument>> configure) => Set(configure, "any_of");
	public IntervalsDescriptor<TDocument> Fuzzy(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy intervalsFuzzy) => Set(intervalsFuzzy, "fuzzy");
	public IntervalsDescriptor<TDocument> Fuzzy(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzyDescriptor<TDocument>> configure) => Set(configure, "fuzzy");
	public IntervalsDescriptor<TDocument> Match(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch intervalsMatch) => Set(intervalsMatch, "match");
	public IntervalsDescriptor<TDocument> Match(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatchDescriptor<TDocument>> configure) => Set(configure, "match");
	public IntervalsDescriptor<TDocument> Prefix(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix intervalsPrefix) => Set(intervalsPrefix, "prefix");
	public IntervalsDescriptor<TDocument> Prefix(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefixDescriptor<TDocument>> configure) => Set(configure, "prefix");
	public IntervalsDescriptor<TDocument> Wildcard(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard intervalsWildcard) => Set(intervalsWildcard, "wildcard");
	public IntervalsDescriptor<TDocument> Wildcard(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcardDescriptor<TDocument>> configure) => Set(configure, "wildcard");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class IntervalsDescriptor : SerializableDescriptor<IntervalsDescriptor>
{
	internal IntervalsDescriptor(Action<IntervalsDescriptor> configure) => configure.Invoke(this);

	public IntervalsDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private IntervalsDescriptor Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private IntervalsDescriptor Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public IntervalsDescriptor AllOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOf intervalsAllOf) => Set(intervalsAllOf, "all_of");
	public IntervalsDescriptor AllOf<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAllOfDescriptor> configure) => Set(configure, "all_of");
	public IntervalsDescriptor AnyOf(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOf intervalsAnyOf) => Set(intervalsAnyOf, "any_of");
	public IntervalsDescriptor AnyOf<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsAnyOfDescriptor> configure) => Set(configure, "any_of");
	public IntervalsDescriptor Fuzzy(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzy intervalsFuzzy) => Set(intervalsFuzzy, "fuzzy");
	public IntervalsDescriptor Fuzzy<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsFuzzyDescriptor> configure) => Set(configure, "fuzzy");
	public IntervalsDescriptor Match(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatch intervalsMatch) => Set(intervalsMatch, "match");
	public IntervalsDescriptor Match<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsMatchDescriptor> configure) => Set(configure, "match");
	public IntervalsDescriptor Prefix(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefix intervalsPrefix) => Set(intervalsPrefix, "prefix");
	public IntervalsDescriptor Prefix<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsPrefixDescriptor> configure) => Set(configure, "prefix");
	public IntervalsDescriptor Wildcard(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcard intervalsWildcard) => Set(intervalsWildcard, "wildcard");
	public IntervalsDescriptor Wildcard<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.IntervalsWildcardDescriptor> configure) => Set(configure, "wildcard");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}